package br.ufal.ic.fracao.agent;

import jade.Boot;
import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.core.Runtime;
import jade.wrapper.AgentContainer;
import jade.wrapper.AgentController;
import jade.wrapper.StaleProxyException;
import br.ufal.ic.fracao.agent.avaliation.AvaliatorAgent;
import br.ufal.ic.fracao.agent.resolution.CoordinatorAgent;
import br.ufal.ic.fracao.agent.resolution.operation.addition.AdditionAgent;
import br.ufal.ic.fracao.agent.resolution.operation.exponentiation.ExponentiationAgent;
import br.ufal.ic.fracao.agent.resolution.operation.multiplication.MultiplicationAgent;
import br.ufal.ic.fracao.agent.resolution.operation.simplification.SimplificationAgent;
import br.ufal.ic.fracao.agent.resolution.operation.subtraction.SubtractionAgent;

public class Facade {
    
    private static Facade instance;
    private CoordinatorAgent coordinatorAgent;
    private AvaliatorAgent avaliatorAgent;
    private SimplificationAgent simplificationAgent;
    private ExponentiationAgent exponentiationAgent;
    private MultiplicationAgent multiplicationAgent;
    private AdditionAgent additionAgent;
    private SubtractionAgent subtractionAgent;
    
    public static Facade getInstance(){
        if(instance == null){
            instance = new Facade();
        }
        return instance;
    }
    
    private Facade(){
        this.startAgents();
        this.setCoordinatorAgent();
        this.setAvaliatorAgent();
    }
    
    private void startAgents(){
        Boot.main(new String[]{"-gui"});
        Boot.main(new String[]{"-container", "SimplificationAgent:br.ufal.ic.fracao.agent.resolution.operation.simplification.SimplificationAgent"});
        Boot.main(new String[]{"-container", "ExponentiationAgent:br.ufal.ic.fracao.agent.resolution.operation.exponentiation.ExponentiationAgent"});
        Boot.main(new String[]{"-container", "MultiplicationAgent:br.ufal.ic.fracao.agent.resolution.operation.multiplication.MultiplicationAgent"});
        Boot.main(new String[]{"-container", "AdditionAgent:br.ufal.ic.fracao.agent.resolution.operation.addition.AdditionAgent"});
        Boot.main(new String[]{"-container", "SubtractionAgent:br.ufal.ic.fracao.agent.resolution.operation.subtraction.SubtractionAgent"});
       	
        //operationAgent();
    }
    
    /*private void operationAgent(){
        Runtime runtime = Runtime.instance();
        
        simplificationAgent = new SimplificationAgent();
        exponentiationAgent = new ExponentiationAgent();
        multiplicationAgent = new MultiplicationAgent();
        additionAgent = new AdditionAgent();
        subtractionAgent = new SubtractionAgent();
        
        Profile profile = new ProfileImpl();
        profile.setParameter(Profile.CONTAINER_NAME, "Operation");
        
        AgentContainer controllerAgentContainer = runtime.createAgentContainer(profile);
        
        try {
            AgentController controller;
            controller  = controllerAgentContainer.acceptNewAgent("SimplificationAgent", simplificationAgent);
            controller  = controllerAgentContainer.acceptNewAgent("ExponentiationAgent", exponentiationAgent);
            controller  = controllerAgentContainer.acceptNewAgent("MultiplicationAgent", multiplicationAgent);
            controller  = controllerAgentContainer.acceptNewAgent("AdditionAgent", additionAgent);
            controller  = controllerAgentContainer.acceptNewAgent("SubtractionAgent", subtractionAgent);
            
            controller.start();
        } catch (StaleProxyException ex) {
            System.out.println("Agente n�o pode ser iniciado");
        }
    }*/
    
    private void setCoordinatorAgent(){
        Runtime runtime = Runtime.instance();
        
        coordinatorAgent = new CoordinatorAgent();
        
        Profile profile = new ProfileImpl();
        profile.setParameter(Profile.CONTAINER_NAME, "Coordination");
        
        AgentContainer controllerAgentContainer = runtime.createAgentContainer(profile);
        
        try {
            AgentController controller;
            controller  = controllerAgentContainer.acceptNewAgent("CoordinatorAgent", coordinatorAgent);
            controller.start();
        } catch (StaleProxyException ex) {
            System.out.println("Agente n�o pode ser iniciado");
        }
    }
    
    private void setAvaliatorAgent(){
        Runtime runtime = Runtime.instance();
        
        avaliatorAgent = new AvaliatorAgent();
        
        Profile profile = new ProfileImpl();
        profile.setParameter(Profile.CONTAINER_NAME, "Avaliation");
        
        AgentContainer controllerAgentContainer = runtime.createAgentContainer(profile);
        
        try {
            AgentController controller;
            controller  = controllerAgentContainer.acceptNewAgent("AvaliatorAgent", avaliatorAgent);
            controller.start();
        } catch (StaleProxyException ex) {
            System.out.println("Agente n�o pode ser iniciado");
        }
    }
    
    public CoordinatorAgent getCoordinatorAgent() {
        return coordinatorAgent;
    }
    
    public AvaliatorAgent getAvaliatorAgent() {
        return avaliatorAgent;
    }
    
    public void updateResult(){
    	this.coordinatorAgent.setResult("");
    	this.avaliatorAgent.setResult("");
    }
    
    public String getExplication(){
    	return this.coordinatorAgent.getExplication();
    }
    
    public boolean getAcertou(){
    	return this.avaliatorAgent.isAcertou();
    }
}
